﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using snat.model;
using snat.proxy.items;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using snat.model.EventFramework;

namespace snat.algorithms.node.TFIDF {


    public delegate void finished();

    public class TFIDFNode : BaseNode {

        public static IDF global = new IDF();
        private Dictionary<int, TF> local = new Dictionary<int, TF>();
        public static finished finishedEvent = null;
        private static EventHandler<FinishArgs> listener = new EventHandler<FinishArgs>(EndOfShow);
        
       

        public TFIDFNode(int _id, string _name, List<Edge> _inEdges = null, List<Edge> _outEdges = null)
            : base(_id, _name, _inEdges, _outEdges) {
                onSend += updateTFIDGTable;

                //Todo find a more elegant solution to ensure only one of these handlers is added
                NetworkModel.Instance.onFinish -= EndOfShow;
                NetworkModel.Instance.onFinish += EndOfShow;
        }

        private void updateTFIDGTable(Message m, List<BaseNode> recipients) {
            Console.WriteLine(m.Data);
            JObject o = JObject.Parse(m.Data);
            local.Add(m.ID, new TF(JsonConvert.DeserializeObject<Dictionary<String, int>>(o["BOW"].ToString()),
                            JsonConvert.DeserializeObject<int>(o["DL"].ToString())));
            global.updateScore(JsonConvert.DeserializeObject<Dictionary<String, int>>(o["BOW"].ToString()),
                            JsonConvert.DeserializeObject<int>(o["DL"].ToString()));
        }

        private static void EndOfShow(object src, FinishArgs args) {

            foreach (String word in global.WordCount.Keys.OrderBy(x => global.WordCount[x])) {
                Console.WriteLine("{0}, {1}", word, global.WordCount[word]);
            }
            Dictionary<String, int> TFIDF = new Dictionary<String, int>();
            foreach (String word in global.AppearenceCount.Keys) {
                TFIDF[word] = 
            }
            //for all words in network
            //calculate TFIDF score
            //print out the maximums...woop ???
        }

    }

}


